package com.ruoyi.rycc.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.rycc.domain.CcCourseware;
import com.ruoyi.rycc.service.ICcCoursewareService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

/**
 * 课件Controller
 *
 * @author antg
 * @date 2021-09-06
 */
@Controller
@RequestMapping("/courseware/courseware")
public class CcCoursewareController extends BaseController {
    private String prefix = "courseware/courseware";

    @Autowired
    private ICcCoursewareService ccCoursewareService;

    @RequiresPermissions("courseware:courseware:view")
    @GetMapping("/{chapterId}")
    public String courseware(@PathVariable("chapterId") Long chapterId, ModelMap mmap) {
        mmap.put("chapterId", chapterId);
        return prefix + "/courseware";
    }

    /**
     * 查询课件列表
     */
    @RequiresPermissions("courseware:courseware:list")
    @PostMapping("/list/{chapterId}")
    @ResponseBody
    public TableDataInfo list(@PathVariable("chapterId") Long chapterId, CcCourseware ccCourseware) {
        startPage();
        ccCourseware.setChapterId(chapterId);
        List<CcCourseware> list = ccCoursewareService.selectCcCoursewareList(ccCourseware);
        return getDataTable(list);
    }

    /**
     * 导出课件列表
     */
    @RequiresPermissions("courseware:courseware:export")
    @Log(title = "课件", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(CcCourseware ccCourseware) {
        List<CcCourseware> list = ccCoursewareService.selectCcCoursewareList(ccCourseware);
        ExcelUtil<CcCourseware> util = new ExcelUtil<CcCourseware>(CcCourseware.class);
        return util.exportExcel(list, "课件数据");
    }

    /**
     * 新增课件
     */
    @GetMapping("/add/{chapterId}")
    public String add(@PathVariable("chapterId") Long chapterId, ModelMap mmap) {
        mmap.put("chapterId", chapterId);
        return prefix + "/add";
    }

    /**
     * 新增保存课件
     */
    @RequiresPermissions("courseware:courseware:add")
    @Log(title = "课件", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(CcCourseware ccCourseware, @RequestParam(value = "coursewareFile", required = false) MultipartFile coursewareFile) throws IOException {
        if (coursewareFile != null) {
            String ccCoursewarePath = FileUploadUtils.upload(RuoYiConfig.getUploadPath(), coursewareFile);
            ccCourseware.setCoursewareUrl(ccCoursewarePath);
            long size = coursewareFile.getSize() / 1024;
            if (size < 1) {
                size = 1;
            }
            ccCourseware.setCoursewareSize(size);
            String[] temp = ccCoursewarePath.split("\\.");
            ccCourseware.setCoursewareType(temp[temp.length - 1]);
        }
        return toAjax(ccCoursewareService.insertCcCourseware(ccCourseware));
    }

    /**
     * 修改课件
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        CcCourseware ccCourseware = ccCoursewareService.selectCcCoursewareById(id);
        mmap.put("ccCourseware", ccCourseware);
        return prefix + "/edit";
    }

    /**
     * 修改保存课件
     */
    @RequiresPermissions("courseware:courseware:edit")
    @Log(title = "课件", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(CcCourseware ccCourseware, @RequestParam(value = "coursewareFile", required = false) MultipartFile coursewareFile) throws IOException {
        if (coursewareFile != null) {
            String ccCoursewarePath = FileUploadUtils.upload(RuoYiConfig.getUploadPath(), coursewareFile);
            ccCourseware.setCoursewareUrl(ccCoursewarePath);
            long size = coursewareFile.getSize() / 1024;
            if (size < 1) {
                size = 1;
            }
            ccCourseware.setCoursewareSize(size);
            String[] temp = ccCoursewarePath.split("\\.");
            ccCourseware.setCoursewareType(temp[temp.length - 1]);
        }
        return toAjax(ccCoursewareService.updateCcCourseware(ccCourseware));
    }

    /**
     * 删除课件
     */
    @RequiresPermissions("courseware:courseware:remove")
    @Log(title = "课件", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(ccCoursewareService.deleteCcCoursewareByIds(ids));
    }
}
